import { useReactive } from "ahooks";
import { useModel } from "umi";
import { v4 as uuidv4 } from "uuid";
import compress from "gql-compress";

const useCreateStaff = () => {
    const { graphqlFetch } = useModel("global");

    const state = useReactive({
        loading: false,
    });

    const createStaff = async (values, callback) => {
        state.loading = true;
        let variables = {
            osUuid: uuidv4(),
            osName: values.osName.trim(),
            osType: values.osType,
            pyName: pinyin(values.osName.trim(), { toneType: "none", nonZh: "consecutive" }),
            osRoge: values.osRoge || null,
            osPhon: values.osPhon?.trim() || null,
            osNati: values.osNati || null,
            osIdnu: values.osIdnu || null,
            opUuid: values.opUuid || null,
            deInfo: values.deInfo || {},
            osFrom: "手工录入",
        };
        let res = await graphqlFetch({
            query,
            variables,
        });
        res && callback && callback(res);
        state.loading = false;
    };

    return { ...state, createStaff };
};

let query = compress(`mutation MyMutation(
    $osUuid: UUID!
    $osType: String!
    $osName: String!
    $pyName: String!
    $osRoge: String!
    $osPhon: String
    $osNati: String
    $osIdnu: String
    $opUuid: UUID
    $deInfo: JSON
    $osFrom: String!
  ) {
    createOrganizationStaff(
        input: {
            organizationStaff: {
                osUuid: $osUuid
                osName: $osName
                osType: $osType
                pyName: $pyName
                osRoge: $osRoge
                osPhon: $osPhon
                osNati: $osNati
                osIdnu: $osIdnu
                opUuid: $opUuid
                deInfo: $deInfo
                osFrom: $osFrom
            }
        }
    ) {
        clientMutationId
    }
}`);

export default useCreateStaff;
